/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uloha4.parser;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import uloha4.entities.Node;

/**
 *
 * @author Tomique
 */
public class GMLParser {

    public static ArrayList<Node> parse(String filepath) {

        ArrayList<Node> list = new ArrayList<>(1589);

        try {
            BufferedReader br = new BufferedReader(new FileReader(filepath));

            String line = br.readLine();

            int bracketOpen = 0;
            int bracketClose = 0;

            boolean directed = false;
            int nodeCount = 0;
            int edgeCount = 0;

            do {

                if (line.equals("graph")) {
                    line = br.readLine();
                    if (line.equals("[")) {
                        ++bracketOpen;
                        line = br.readLine();
                        StringTokenizer st = new StringTokenizer(line);
                        if (st.nextToken().equals("directed")) {
                            directed = st.nextToken().equals("0") ? false : true;
                        }

                    } else {
                        throw new RuntimeException("Invalid format!");
                    }
                    continue;
                }
                StringTokenizer st = new StringTokenizer(line);
                switch (st.nextToken()) {
                    case "node":
                        st = new StringTokenizer(br.readLine());
                        if (st.nextToken().equals("[")) {
                            ++nodeCount;
                            st = new StringTokenizer(br.readLine());
                            // id
                            int id = -1;
                            if (st.nextToken().equals("id")) {
                                id = Integer.parseInt(st.nextToken());
                            } else {
                                throw new RuntimeException("Invalid format!");
                            }
                            // label
                            line = br.readLine();
                            st = new StringTokenizer(line, " ");
                            if (st.nextToken().equals("label")) {
                                st = new StringTokenizer(line, "\"");
                                st.nextToken();
                                String label = st.nextToken();
                                list.add(id, new Node(label, id));
                            } else {
                                throw new RuntimeException("Invalid format!");
                            }
                            // ] bracket
                            st = new StringTokenizer(br.readLine());
                            if (!st.nextToken().equals("]")) {
                                throw new RuntimeException("Invalid format!");
                            }
                        }

                        break;
                    case "edge":
                        int source,
                         target;
                        double value;
                        st = new StringTokenizer(br.readLine());
                        if (st.nextToken().equals("[")) {
                            // source
                            st = new StringTokenizer(br.readLine());
                            if (st.nextToken().equals("source")) {
                                source = Integer.parseInt(st.nextToken());
                            } else {
                                throw new RuntimeException("Invalid format!");
                            }
                            // target
                            st = new StringTokenizer(br.readLine());
                            if (st.nextToken().equals("target")) {
                                target = Integer.parseInt(st.nextToken());
                            } else {
                                throw new RuntimeException("Invalid format!");
                            }
                            // value
                            st = new StringTokenizer(br.readLine());
                            if (st.nextToken().equals("value")) {
                                value = Double.parseDouble(st.nextToken());
                            } else {
                                throw new RuntimeException("Invalid format!");
                            }

                        } else {
                            throw new RuntimeException("Invalid format!");
                        }
                        // ] bracket
                        st = new StringTokenizer(br.readLine());
                        if (!st.nextToken().equals("]")) {
                            throw new RuntimeException("Invalid format!");
                        }
                        // propojim odpovidajici uzly orientovanou hranou
                        list.get(source).addDirectedNeighbour(list.get(target));

                        // propojim sousedy s hranou (neorientovanou)
                        list.get(source).addNeighbour(list.get(target));
                        list.get(target).addNeighbour(list.get(source));

                        break;
                    default:
                }

            } while ((line = br.readLine()) != null);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(GMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
}
